package contactbook.test.mock.entitymanager;

import java.io.Serializable;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.FlushModeType;
import javax.persistence.LockModeType;
import javax.persistence.Query;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class EntityManagerMock implements EntityManager {
	
	@Override
	public Query createQuery(String arg0) {
		return new QueryMock(arg0);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public <T> T find(Class<T> arg0, Object arg1) {
		Transaction tx = null;
		Session session = SessionFactoryUtil.getInstance().getCurrentSession();
		T object = null;
		try {
			tx = session.beginTransaction();
			object = (T) session.get(arg0, (Serializable)arg1);
			tx.commit();
		} catch(RuntimeException ex) {
			 if (tx != null && tx.isActive()) {
				 try {
					 tx.rollback();
				 } catch (HibernateException e1) {
				 }
				 throw ex;
			 }
		}
		return object;
	}

	@Override
	public void flush() {
		Transaction tx = null;
		Session session = SessionFactoryUtil.getInstance().getCurrentSession();
		try {
			tx = session.beginTransaction();
			session.flush();
			tx.commit();
		} catch(RuntimeException ex) {
			 if (tx != null && tx.isActive()) {
				 try {
					 tx.rollback();
				 } catch (HibernateException e1) {
				 }
				 throw ex;
			 }
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public <T> T merge(T object) {
		Transaction tx = null;
		Session session = SessionFactoryUtil.getInstance().getCurrentSession();
		T obj = null;
		try {
			tx = session.beginTransaction();
			obj = (T) session.merge(object);
			tx.commit();
		} catch(RuntimeException ex) {
			 if (tx != null && tx.isActive()) {
				 try {
					 tx.rollback();
				 } catch (HibernateException e1) {
				 }
				 throw ex;
			 }
		}
		return obj;
	}

	@Override
	public void persist(Object object) {
		Transaction tx = null;
		Session session = SessionFactoryUtil.getInstance().getCurrentSession();
		try {
			tx = session.beginTransaction();
			session.save(object);
			tx.commit();
		} catch(RuntimeException ex) {
			 if (tx != null && tx.isActive()) {
				 try {
					 tx.rollback();
				 } catch (HibernateException e1) {
				 }
				 throw ex;
			 }
		}
	}

	@Override
	public void remove(Object object) {
		Transaction tx = null;
		Session session = SessionFactoryUtil.getInstance().getCurrentSession();
		try {
			tx = session.beginTransaction();
			session.delete(object);
			tx.commit();
		} catch(RuntimeException ex) {
			 if (tx != null && tx.isActive()) {
				 try {
					 tx.rollback();
				 } catch (HibernateException e1) {
				 }
				 throw ex;
			 }
		}
	}
	
	
	
	
	@Override
	public void clear() {
	}

	@Override
	public void close() {
	}

	@Override
	public boolean contains(Object arg0) {
		return false;
	}

	@Override
	public Query createNamedQuery(String arg0) {
		return null;
	}

	@Override
	
	public Query createNativeQuery(String arg0) {
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Query createNativeQuery(String arg0, Class arg1) {
		return null;
	}

	@Override
	public Query createNativeQuery(String arg0, String arg1) {
		return null;
	}

	@Override
	public Object getDelegate() {
		return null;
	}

	@Override
	public FlushModeType getFlushMode() {
		return null;
	}

	@Override
	public <T> T getReference(Class<T> arg0, Object arg1) {
		return null;
	}

	@Override
	public EntityTransaction getTransaction() {
		return null;
	}

	@Override
	public boolean isOpen() {
		return false;
	}

	@Override
	public void joinTransaction() {
	}

	@Override
	public void lock(Object arg0, LockModeType arg1) {
	}

	@Override
	public void refresh(Object arg0) {
		
	}

	@Override
	public void setFlushMode(FlushModeType arg0) {
	}

}

